﻿using AiXiu.BLL;
using AiXiu.Common;
using AiXiu.IBLL;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AiXiu.Web
{
    public partial class Videos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 查询用户资料
                int otherId = int.Parse(Request.QueryString["id"]);
                IUserManager userManager = new UserManager();
                TBUsers other = userManager.GetUser(otherId).ReadResult();
                // 显示用户资料
                this.lblNickName.Text = other.NickName ?? $"用户{other.Id}";
                switch (other.Sex)
                {
                    case 1:
                        this.lblSex.Text = "小哥哥";
                        break;
                    case 2:
                        this.lblSex.Text = "小姐姐";
                        break;
                    default:
                        this.lblSex.Visible = false;
                        break;
                }
                this.lblAddress.Text = other.Address ?? "未知地";
                this.lblBirthday.Text = other.Birthday.HasValue ? other.Birthday.Value.ToString("M") : "未设置";
                this.lblHobby.Text = other.Hobby ?? "未添加";
                // 如果为本人，则隐藏好友及聊天按钮
                int selfId = IdentityManager.ReadUser().Id;
                if (selfId.Equals(otherId))
                {
                    this.btnFriend.Enabled = false;
                    this.btnMessage.Enabled = false;
                    this.btnFriendBlock.Visible = false;
                }
                // 判断是否是好友
                IUserManager user = new UserManager();
                bool isFriend = user.IsFriend(selfId, otherId);
                if (isFriend)
                {
                    this.btnFriend.Enabled = false;
                    this.btnFriend.Visible = false;
                }
                else
                {
                    this.btnMessage.Enabled = false;
                    this.btnMessage.Visible = false;
                }
                // 展示视频列表
                IUserManager manager = new UserManager();
                List<TBVideos> videos = manager.GetVideosid(otherId).ReadResult();
                rptVideos.DataSource = videos;
                rptVideos.DataBind();
            }
        }

        protected void btnFriend_Click(object sender, EventArgs e)
        {
            // 不能添加自己为好友
            int selfId = IdentityManager.ReadUser().Id;
            int otherId = int.Parse(Request.QueryString["id"]);
            if (selfId.Equals(otherId))
            {
                this.Alert("frdfail", "不能添加自己为好友！");
                return;
            }
            // 添加好友
            IUserManager userManager = new UserManager();
            bool friendResult = userManager.AddFriend(selfId, otherId);
            if (!friendResult)
            {
                // 提示添加失败
                this.Alert("frderror", "添加失败！");
            }
            else
            {
                // 提示并跳转页面
                this.AlertAndRedirect("Friends.aspx", "添加成功！", $"Chat.aspx?to={otherId}");
            }
        }
        /// <summary>
        /// 立即聊天
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnMessage_Click(object sender, EventArgs e)
        {
            int otherId = int.Parse(Request.QueryString["id"]);
            this.Redirect("frdredirect", $"Chat.aspx?to={otherId}");
        }
    }
}